package com.ying.tests.AAAmode.singleton;

public class LazySingletonTest {

    public static void main(String[] args) {
        LazySingletonService instance = LazySingletonService.getInstance();
        instance.getName();
        LazySingletonService instance2 = LazySingletonService.getInstance();
        instance2.getName();
        // 比较地址，是否是同一个实例
        if (instance == instance2) {
            System.out.println("同一个实例");
        } else {
            System.out.println("不同的实例");
        }
    }

}

class LazySingletonService {
    private static volatile LazySingletonService instance = null;

    private LazySingletonService() {
        System.out.println("竞选总统");
    }

    public void getName() {
        System.out.println("大家好，我是美国总统，特朗普");
    }

    public static synchronized LazySingletonService getInstance() {
        if (null == instance) {
            instance = new LazySingletonService();
        } else {
            System.out.println("上一任总统还没下岗，你急个锤锤");
        }
        return instance;
    }

}
